import { reactive, readonly } from 'vue'
import { getConfig } from '@/api/cms/system/index'
import request from '@/config/axios'
export const imageUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-image'
export const simpleImageUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-image-simple'
export const avatarUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload'
export const videoUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-video'

  export const simpleVideoUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-video-simple'

  export const audioUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-audio'
export const docUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-doc'
export const fileUploadUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/upload-file'
export const docImportUrl =
  import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/infra/file/import-doc'

export const downRemoteImage = async (url: string): Promise<any> => (await request.post({url:'/infra/file/image-remote-down', data:url,  headersType: 'text/plain' })).data;

  export interface UploadSettings {
  imageTypes: string
  imageInputAccept: string
  videoInputAccept: string
  audioInputAccept: string
  libraryInputAccept: string
  docInputAccept: string
  attachInputAccept: string
  imageLimitByte: number
  videoLimitByte: number
  audioLimitByte: number
  libraryLimitByte: number
  docLimitByte: number
  attachLimitByte: number
}
export interface BaseSettings {
  filesExtensionBlacklist: string
  uploadsExtensionBlacklist: string
}

export interface SecuritySettings {
  passwordMinLength: number
  passwordMaxLength: number
  passwordStrength: number
  passwordPattern: string
  firstLoginChangePasssword: Boolean
  resetChangePasssword: Boolean
  weakPasswordCheck: Boolean
  weakPasswordProcessMode: number
  inactivityLogoutEnable: Boolean
  inactivityLogoutTime: number
  multiLoginEnable: Boolean
  adminIdleLock: Boolean
  adminIdleDays: number
  passwordMaxDays: number
  passwordWarnDays: number
  twoFactor: Boolean
}
export interface RegisterSettings {
  enabled: Boolean
}
export interface Config {
  base: BaseSettings
  upload: UploadSettings
  security: SecuritySettings
  register: RegisterSettings
}

const state = reactive<Config>({
  base: {
    filesExtensionBlacklist: 'exe,com,bat,jsp,jspx,asp,aspx,php',
    uploadsExtensionBlacklist: 'exe,com,bat,jsp,jspx,asp,aspx,php,html,htm,xhtml,xml,shtml,shtm'
  },
  upload: {
    imageTypes: 'jpg,jpeg,png,gif',
    imageInputAccept: '.jpg,.jpeg,.png,.gif',
    videoInputAccept: '.mp4,.m3u8',
    audioInputAccept: '.mp3,.ogg,.wav',
    libraryInputAccept: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx',
    docInputAccept: '.doc,.docx,.xls,.xlsx',
    attachInputAccept:
      '.zip,.7z,.gz,.bz2,.iso,.rar,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.jpg,.jpeg,.png,.gif,.mp4,.m3u8,.mp3,.ogg',
    imageLimitByte: 0,
    videoLimitByte: 0,
    audioLimitByte: 0,
    libraryLimitByte: 0,
    docLimitByte: 0,
    attachLimitByte: 0
  },
  security: {
    passwordMinLength: 0,
    passwordMaxLength: 64,
    passwordStrength: 0,
    passwordPattern: '.*',
    firstLoginChangePasssword: false,
    resetChangePasssword: false,
    weakPasswordCheck: false,
    weakPasswordProcessMode: 1,
    inactivityLogoutEnable: false,
    inactivityLogoutTime: 30,
    multiLoginEnable: true,
    adminIdleLock: false,
    adminIdleDays: 180,
    passwordMaxDays: 0,
    passwordWarnDays: 0,
    twoFactor: false
  },
  register: {
    enabled: false
  }
})
export const baseSettings = readonly(state).base
export const uploadSettings = readonly(state).upload
export const securitySettings = readonly(state).security
export const initConfig = async () => {
  const config = await getConfig()
  Object.assign(state.base, config)
  Object.assign(state.upload, config.uploadConfig)
  Object.assign(state.register, config.registerConfig)
  Object.assign(state.security, config.securityConfig)
}
initConfig()
